package Server.Login.Controller;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import Server.Login.View.*;
import Server.Login.Model.*;

import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.eclipse.swt.widgets.FileDialog;

public class LoadFileButtonListener implements ActionListener 
{
	public LoadFileButtonListener(ServerLoginView view, ServerLoginModel model) 
	{
		this.view = view;
		this.model = model;
	}

	public void actionPerformed(ActionEvent event)
	{
		JFileChooser chooser = new JFileChooser();
		FileNameExtensionFilter filter = new FileNameExtensionFilter("txt file", "txt");
		chooser.setFileFilter(filter);
		chooser.setAcceptAllFileFilterUsed(false);
		int option = chooser.showOpenDialog(view.frame);

		if (option == JFileChooser.APPROVE_OPTION) 
		{

			try 
			{
				BufferedReader in = new BufferedReader(new FileReader(chooser
						.getSelectedFile().getAbsolutePath()));
				view.userNameTextField.setText(in.readLine());
				view.passwordField.setText(in.readLine());
			}
			catch (FileNotFoundException exception) 
			{
				System.out.println("FileNotFound : "
						+ chooser.getSelectedFile().getAbsolutePath());
				exception.printStackTrace();
			} 
			catch (IOException exception) 
			{
				System.out.println("IOException when read file "
						+ chooser.getSelectedFile().getAbsolutePath());
				exception.printStackTrace();
			}
		}
	}

	private ServerLoginView view;

	private ServerLoginModel model;
}